#include "request_sender.h"

RequestSender::RequestSender(ClientNetwork *network,QObject *parent) : QObject(parent),network(network)
{
    this->network = network;
}

RequestSender::~RequestSender()
{
    network->deleteLater();
}



void RequestSender::sendLoginRequest(const QString &username, const QString &password)
{
    QByteArray data = MessageUtil::buildLoginRequest(username, password);
    network->sendMessage(data);
}

void RequestSender::sendRegisterRequest(const QString &username, const QString &password)
{
    QJsonObject data;
    data["username"] = username;
    data["password"] = password;
    QByteArray request = MessageUtil::serialize(REGISTER_REQUEST, data);
    network->sendMessage(request);
}

// void RequestSender::sendPrivateChat(const QString &sender, const QString &receiver, const QString &text)
// {
//     QByteArray data = MessageUtil::buildPrivateChat(sender, receiver, text);
//     network->sendMessage(data);
// }

void RequestSender::sendGroupChat(const QString &sender, const QString &groupId, const QString &text)
{
    QJsonObject data;
    data["sender"] = sender;
    data["groupId"] = groupId;
    data["text"] = text;
    data["timestamp"] = QDateTime::currentMSecsSinceEpoch();
    QByteArray request = MessageUtil::serialize(GROUP_CHAT, data);
    network->sendMessage(request);
}

void RequestSender::sendAddFriendRequest(const QString &userId, const QString &friendId)
{
    QJsonObject data;
    data["userId"] = userId;
    data["friendId"] = friendId;
    QByteArray request = MessageUtil::serialize(ADD_FRIEND, data);
    network->sendMessage(request);
}

void RequestSender::sendRemoveFriendRequest(const QString &userId, const QString &friendId)
{
    QJsonObject data;
    data["userId"] = userId;
    data["friendId"] = friendId;
    QByteArray request = MessageUtil::serialize(REMOVE_FRIEND, data);
    network->sendMessage(request);
}

// void RequestSender::sendCreateGroupRequest(const QString &userId, const QString &groupName)
// {
//     QJsonObject data;
//     data["userId"] = userId;
//     data["groupName"] = groupName;
//     QByteArray request = MessageUtil::serialize(CTREAT_GROUP, data);
//     network->sendMessage(request);
// }

void RequestSender::sendAddGroupMemberRequest(const QString &groupId, const QString &memberId)
{
    QJsonObject data;
    data["groupId"] = groupId;
    data["memberId"] = memberId;
    QByteArray request = MessageUtil::serialize(ADD_GROUP_MEMBER, data);
    network->sendMessage(request);
}

void RequestSender::sendRemoveGroupMemberRequest(const QString &groupId, const QString &memberId)
{
    QJsonObject data;
    data["groupId"] = groupId;
    data["memberId"] = memberId;
    QByteArray request = MessageUtil::serialize(REMOVE_GROUP_MEMBER, data);
    network->sendMessage(request);
}

void RequestSender::sendGetGroupMessagesRequest(const QString &groupId)
{
    QJsonObject data;
    data["groupId"] = groupId;
    QByteArray request = MessageUtil::serialize(GET_GROUP_MESSAGES, data);
    network->sendMessage(request);
}
